#include "C_PeriodicTable.h"


C_PeriodicTable::C_PeriodicTable()
{
}


C_PeriodicTable::~C_PeriodicTable()
{
}

void C_PeriodicTable::loadFromFile(std::string filename)
{
	//open file
	FILE *pFile = fopen(filename.c_str(), "r");

	json_t			*rootJSON;
	json_error_t	errorJSON;

	//load from file
	rootJSON = json_loadf(pFile, 0, &errorJSON);

	//test
	EX_R(!rootJSON, "[JSON]:" + std::to_string(errorJSON.line) + errorJSON.text);

	//loop array
	for (int i = 0; i < json_array_size(rootJSON); i++)
	{
		//load object of array
		json_t *data = json_array_get(rootJSON, i);
		//test
		EX_R(!json_is_object(data), "[JSON]: Element " + std::to_string(i) + " is not exist!");

		this->parseElement(data);
	}
	//close
	json_decref(rootJSON);
}

void C_PeriodicTable::parseElement(json_t *data)
{
	S_JSON_Element elem;
	std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;

	/////////////////////////////////////////////////////////
	//////// load glyph
	elem.glyph = json_object_get(data, "glyph");
	EX_R(!json_is_string(elem.glyph), "[JSON]: Glyph cannot load!");

	elem.name = json_object_get(data, "name");
	EX_R(!json_is_string(elem.name), "[JSON]: Name cannot load!");

	elem.weight = json_object_get(data, "weight");
	EX_R(!json_is_real(elem.weight), "[JSON]: Weight cannot load!");

	elem.appearance = json_object_get(data, "appearance");
	EX_R(!json_is_string(elem.appearance), "[JSON]: Appearance cannot load!");

	elem.energy = json_object_get(data, "energy");
	EX_R(!json_is_number(elem.energy), "[JSON]: Energy cannot load!");

	elem.mohs = json_object_get(data, "mohs");
	EX_R(!json_is_number(elem.mohs), "[JSON]: Mohs cannot load!");

	elem.xray = json_object_get(data, "xray");
	EX_R(!json_is_number(elem.xray), "[JSON]: Xray cannot load!");

	elem.melting = json_object_get(data, "melting");
	EX_R(!json_is_number(elem.melting), "[JSON]: Melting cannot load!");

	elem.melEnergy = json_object_get(data, "m_energy");
	EX_R(!json_is_real(elem.melEnergy), "[JSON]: Melting energy cannot load!");

	elem.refraction = json_object_get(data, "refraction");
	EX_R(!json_is_number(elem.refraction), "[JSON]: Refraction cannot load!");

	elem.resistivity = json_object_get(data, "resistivity");
	EX_R(!json_is_number(elem.resistivity), "[JSON]: Resistivity cannot load!");

	elem.per_star = json_object_get(data, "per_star");
	EX_R(!json_is_real(elem.per_star), "[JSON]: Per star cannot load!");

	elem.starMin = json_object_get(data, "star_min");
	EX_R(!json_is_real(elem.starMin), "[JSON]: Star min cannot load!");

	elem.starMax = json_object_get(data, "star_max");
	EX_R(!json_is_real(elem.starMax), "[JSON]: Star max cannot load!");

	elem.per_meteor = json_object_get(data, "per_meteor");
	EX_R(!json_is_real(elem.per_meteor), "[JSON]: Per meteor cannot load!");

	elem.meteorMin = json_object_get(data, "meteor_min");
	EX_R(!json_is_real(elem.meteorMin), "[JSON]: Meteor min cannot load!");

	elem.meteorMax = json_object_get(data, "meteor_max");
	EX_R(!json_is_real(elem.meteorMax), "[JSON]: Meteor max cannot load!");

	elem.id = json_object_get(data, "id");
	EX_R(!json_is_number(elem.id), "[JSON]: Id cannot load!");

	C_ChemicalElement *e = new C_ChemicalElement();
	e->set(
		json_string_value(elem.glyph),
		converter.from_bytes(json_string_value(elem.name)),
		json_real_value(elem.weight),
		CHECK_APPEARANCE(std::string(json_string_value(elem.appearance))),
		json_number_value(elem.energy),
		json_number_value(elem.mohs),
		json_number_value(elem.xray),
		json_number_value(elem.melting),
		json_real_value(elem.melEnergy),
		json_number_value(elem.refraction),
		json_number_value(elem.resistivity),
		json_real_value(elem.per_star),
		json_real_value(elem.starMin),
		json_real_value(elem.starMax),
		json_real_value(elem.per_meteor),
		json_real_value(elem.meteorMin),
		json_real_value(elem.meteorMax),
		json_number_value(elem.id)
	);

	m_elements.push_back(e);
}
